import {getCollage} from "../../api/studentApi";
import {addRecord, getChapter, getCourse, getQueType, paper, getQuestions,getChapterTree} from "../../api/exerciseApi";


const state = {
    question_list_load_key:"",

    question_type:[],
    col_cou_list:[],
    cap_list:[],
    paper_que_list:[],
    getPaperInfo:{//获取试卷时的信息
        easyChoose:'0',
        chooseCapId:[]
    },
    total_page:0,
    chapterTreeData : [
        {
            title: '无章节',
            key: '123',
        },
    ],
    questionLoading:false,

    selected_chapter_id:"",
    selected_chapter_name:""
}

const mutations = {
    SET_QUESTION_TYPE:(state, question_type)=>{
        state.question_type = question_type;
        state.question_type.forEach(item=>{
            item.count=0;
            item.max = 0;
            item.id = item.queTypId
        })
    },
    SET_COLLAGE:(state,collage_list)=>{
        state.col_cou_list = []
        collage_list.forEach(item=>{
            state.col_cou_list.push({
                value:item.colId,
                label:item.name,
                children:[{
                    value:'error',
                    label:'课程获取失败'
                }]
            })
        })
    },
    SET_COLLAGE_COURSE:(state, data)=>{
        const {course_list, col_id} = data;
        state.col_cou_list.forEach(item=>{
            if(String(item.value)===String(col_id)){
                item.children=[];
                course_list.forEach(cou_item=>{
                    item.children.unshift({
                        value:cou_item.couId,
                        label:cou_item.name,
                    })
                })
            }
        })
    },
    SET_CAP_LIST:(state, cap_list)=>{
        state.cap_list = cap_list;
    },
    SET_QUESTION_COUNT:(state,data)=>{
        const {quest_type_id, count } = data;
        state.question_type.forEach((item,index)=>{
            if(item.id === quest_type_id){
                const newItem = item;
                newItem.max = item.max+count;
                state.question_type.splice(index, 1, newItem)
            }
        })
    },
    SET_PAPER_QUE_LIST:(state,paper_que_list)=>{
        state.paper_que_list = paper_que_list
    },
    SET_TOTAL_PAGE:(state, totalpage)=>{
        state.total_page = totalpage;
    },
    SET_NOW_COURSE_ID:(state, course_id)=>{
        state.now_course_id = course_id;
    },
    NULL:()=>{}
}

const actions = {
    getQueType({ commit }){ //获取学生信息
        return new Promise((resolve, reject) => {
            getQueType().then(res=>{
                if (res.code === 1) {
                    commit('SET_QUESTION_TYPE', res.data);
                }
                resolve(res);
            }).catch(error => {
                reject(error)
            })
        })
    },
    getCollage({commit}){
        return new Promise((resolve, reject) => {
            getCollage().then(res=>{
                if (res.code === 1) {
                    commit('SET_COLLAGE', res.data);
                }
                resolve(res);
            }).catch(error => {
                reject(error)
            })
        })
    },
    getCourse({commit},col_id){
        return new Promise((resolve, reject) => {
            getCourse({colId:col_id}).then(res=>{
                const data ={
                    col_id:col_id,
                    course_list:res.data
                }
                if (res.code === 1) {
                    commit('SET_COLLAGE_COURSE', data);
                }
                resolve(res);
            }).catch(error => {
                reject(error)
            })
        })
    },
    getCapList({commit,state}, col_id){
        return new Promise((resolve, reject) => {
            let level= state.getPaperInfo.easyChoose;
            if(level==='0') {
                level = null;
            }
            getChapter({courseId:col_id,level:level}).then(res=>{
                if (res.code === 1) {
                    const repdata = res.data;
                    repdata.forEach((item)=>{
                        item.capNum=item.chapter+'.'+item.first+'.'+item.second+'.'+item.third;
                        item.key=item.chaId;
                        item.count = 0;
                        if(item.queTypeCount!==null){
                            item.queTypeCount.forEach(queTypeItem=>{
                                queTypeItem.id = queTypeItem.queTypId;
                                item.count += queTypeItem.count;
                            });
                        }
                    })
                    commit('SET_CAP_LIST', repdata);
                }
                resolve(res);
            }).catch(error => {
                reject(error)
            })
        })
    },
    setQuestionTypeCount({commit},queTypList){
        queTypList.forEach(item=>{
            commit('SET_QUESTION_COUNT',{quest_type_id:item.id,count:item.count});
        })
    },
    getPaper({commit,state}){
        const reqData={
            typeInfoList:[],
            capterIDList:state.getPaperInfo.chooseCapId,
            level:Number(state.getPaperInfo.easyChoose)
        };
        if(reqData.level===0)
            reqData.level=null;
        //开始对类型信息处理
        state.question_type.forEach(item=>{
            const willAddTypeInfo={
                queTypeID:item.id,
                count:item.count===0?item.max===0?0:1:item.count
            }
            reqData.typeInfoList.unshift(willAddTypeInfo);
        })

        return new Promise((resolve, reject) => {
                paper(reqData).then(res=>{
                    if (res.code === 1) {
                        //对题目类型进行处理
                        const resData = res.data;
                        resData.forEach(item=>{
                            state.question_type.forEach(typeItem=>{
                                if(item.type === typeItem.id)
                                    item.type = typeItem.name
                            })
                            if(item.answer==null){
                                item.answer=[]
                            }
                            item.myAns=new Array(item.answer.length===0?0:item.answer.length)
                        })
                        commit('SET_PAPER_QUE_LIST',resData);
                    }
                    resolve(res);
                }).catch(error => {
                    reject(error)
                })
            })
    },
    addRecord({ commit },data){ //添加记录
        const dataTool = new Date();
        const nowDate = dataTool.getFullYear()
            +"-"+((dataTool.getMonth()+1)>=10?(dataTool.getMonth()+1):"0"+(dataTool.getMonth()+1))
            +"-"+(dataTool.getDay()>=10?dataTool.getDate():'0'+dataTool.getDate())+" "
            +(dataTool.getHours()>=10?dataTool.getHours():'0'+dataTool.getHours())+":"
            +(dataTool.getMinutes()>=10?dataTool.getMinutes():'0'+dataTool.getMinutes())+":"
            +(dataTool.getSeconds()>=10?dataTool.getSeconds():'0'+dataTool.getSeconds())

        //'YYYY-MM-DD HH:mm:ss'
        const queData = {
            questionIdList:data.questionIdList,
            createTime: nowDate,
            queRecType:data.queRecType,
            token:data.token,
            studentId:data.studentId,
            right: data.right
        }
        return new Promise((resolve, reject) => {
            addRecord(queData).then(res=>{
                if (res.code === 1) {
                    commit('NULL');
                }
                resolve(res);
            }).catch(error => {
                reject(error)
            })
        })
    },
    getQuestions({commit},data){
        return new Promise((resolve, reject) => {
            getQuestions({courseId:data.courseId,page:data.page,chapterId:data.chapterId}).then(res=>{
                commit('SET_NOW_COURSE_ID',data.courseId);
                if (res.code === 1) {
                    //对题目类型进行处理
                    const resData = res.data;
                    resData.questionList.forEach(item=>{
                        state.question_type.forEach(typeItem=>{
                            if(item.type === typeItem.id)
                                item.type = typeItem.name
                        })
                        if(item.answer==null){
                            item.answer=[]
                        }
                        item.myAns=new Array(item.answer.length===0?0:item.answer.length)
                        if(item.type.search('选')>=0){
                            item.answer.sort((a,b)=>{
                                    return a.content.localeCompare(b.content)
                                }
                            )
                        }
                    })
                    resData.questionList.sort((a,b)=>{
                        return b.type.localeCompare(a.type)
                    })
                    commit('SET_PAPER_QUE_LIST',resData.questionList);
                    commit('SET_TOTAL_PAGE',resData.pages);
                }else{
                    commit('SET_PAPER_QUE_LIST',[]);
                    commit('SET_TOTAL_PAGE',0);
                }
                resolve(res);
            }).catch(error => {
                reject(error)
            })
        })
    },

    getChapterTree({commit},cou_id){
        console.log(cou_id)
        return new Promise((resolve, reject) => {
            getChapterTree({courseId:cou_id}).then(res=>{
                commit("NULL");
                resolve(res);
            }).catch(error => {
                reject(error)
            })
        })
    },
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
